﻿var oEditors = [];

function se2init(id) {
    nhn.husky.EZCreator.createInIFrame({
        oAppRef: oEditors,
        elPlaceHolder: id,
        sSkinURI: "/bbs2/res/se2/SmartEditor2Skin.html",
        htParams: {
            bUseToolbar: true,
            fOnBeforeUnload: function () { },
            fOnAppLoad: function () { },
            fCreator: "createSEditor2"
        }
    });
}

function pasteimage(ev) {
    var textareaId = $("textarea.smarteditor2").attr("id");
    var $this = $(this);
    var original = ev.originalEvent;
    var file = original.clipboardData.items[0].getAsFile();
    var reader = new FileReader();
    reader.onload = function (evt) {
        var result = evt.target.result;
        var arr = result.split(",");
        var data = arr[1]; // raw base64
        var contentType = arr[0].split(";")[0].split(":")[1];
        AjaxJsonCall_Callback("/Image/Paste", { contentType: contentType, data: data }, function (data) { oEditors.getById[textareaId].exec("PASTE_HTML", ["<image src='" + data.url + "'/>"]); });
    };
    reader.readAsDataURL(file);
}

function initPasteImage() {
    $("iframe").contents().find("iframe").contents().find("body").bind("paste", pasteimage);
}

function smartEditor_syncData() {
    var ID = $("textarea.smarteditor2").attr("id");
    oEditors.getById[ID].exec("UPDATE_CONTENTS_FIELD", []);
}

$(function () {
    var list = $("textarea.smarteditor2");
    if (list.length > 0) {
        list.each(function () { var textareaId = $(this).attr("id"); se2init(textareaId); });
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        if (is_chrome) setTimeout("initPasteImage()", 2000);
    }
});
